namespace PickGold.Charting
{
	using System;
	using System.ComponentModel;
	using System.Globalization;

	internal class MarginExpandableObjectConverter : ExpandableObjectConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType));
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			string str = value as string;
			if (str == null)
			{
				return base.ConvertFrom(context, culture, value);
			}
			Margins margins = new Margins();
			string[] strArray = str.Split(new char[] { ',' });
			if (strArray.Length == 4)
			{
				try
				{
					margins.Top = int.Parse(strArray[0].Trim(), CultureInfo.InvariantCulture);
					margins.Bottom = int.Parse(strArray[1].Trim(), CultureInfo.InvariantCulture);
					margins.Left = int.Parse(strArray[2].Trim(), CultureInfo.InvariantCulture);
					margins.Right = int.Parse(strArray[3].Trim(), CultureInfo.InvariantCulture);
					return margins;
				}
				catch
				{
					throw new InvalidOperationException(SR.ExceptionLegendDesignerMarginObjectInvalid(str));
				}
			}
			throw new InvalidOperationException(SR.ExceptionLegendDesignerMarginObjectInvalid(str));
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			Margins margins = value as Margins;
			if ((destinationType == typeof(string)) && (margins != null))
			{
				return string.Format(CultureInfo.InvariantCulture, "{0:D}, {1:D}, {2:D}, {3:D}", new object[] { margins.Top, margins.Bottom, margins.Left, margins.Right });
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
}

